﻿using WebApp.BLL.Base;
using WebApp.DAL;
using WebApp.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApp.BLL.Manage
{
    public partial class S_role_menuService : BaseService<S_role_menu>
    {
        #region 构造

        public S_role_menuService() : base(new S_role_menuRepository()) { }

        #endregion

        #region FindAllRoleid

        public List<S_role_menu> FindAllRoleid(string roleid)
        {
            return this.CurrentRepository.Entities.Where(p => p.Roleid == roleid).ToList();
        }

        #endregion

        #region Save

        public bool Save(string roleid, List<string> menuids)
        {
            //查询角色id的所有权限删除，
            var rolemenuList = this.CurrentRepository.Entities.Where(p => p.Roleid == roleid).ToList();
            if (rolemenuList.Count > 0)
            {
                foreach (var item in rolemenuList)
                {
                    this.CurrentRepository.Delete(item, false);
                }
            }
            if (menuids.Count > 0)
            {
                foreach (string menuid in menuids)
                {
                    S_role_menu rolemenu = new S_role_menu();
                    rolemenu.Roleid = roleid;
                    rolemenu.Menuid = menuid;
                    this.CurrentRepository.Add(rolemenu, false);
                }
            }
            this.CurrentRepository.Save();
            return true;
            
        }

        #endregion

    }
}
